package com.example.service;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HtmlUtil;
import com.example.entity.Account;
import com.example.entity.Category;
import com.example.entity.Introduction;
import com.example.entity.User;
import com.example.exception.CustomerException;
import com.example.mapper.CategoryMapper;
import com.example.mapper.IntroductionMapper;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IntroductionService {
    @Autowired
    private IntroductionMapper introductionMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private UserMapper userMapper;
    public List<Introduction> selectAll(Introduction introduction){
        List<Introduction> introductions = introductionMapper.selectAll(introduction);
        for (Introduction introduction1 : introductions) {
            introduction1.setDescription(HtmlUtil.cleanHtmlTag(introduction1.getContent()));
        }
        return introductions;
//        return introductionMapper.selectAll(introduction);
    }

    public PageInfo<Introduction> selectPage(Integer pageNum, Integer pageSize,Introduction introduction) {
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            introduction.setUserId(currentUser.getId());
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Introduction> list = introductionMapper.selectAll(introduction);
        for (Introduction introduction1 : list) {
            Integer categoryId = introduction1.getCategoryId();
            Integer userId = introduction1.getUserId();
            Category category= categoryMapper.selectById(categoryId);
            User user = userMapper.selectById(userId.toString());
            if (ObjectUtil.isNotEmpty(category)){
                introduction1.setCategoryTitle(category.getTitle());
            }
            if (ObjectUtil.isNotEmpty(user)){
                introduction1.setUserName(user.getName());
            }
        }
        return PageInfo.of(list);

    }

    public void add(Introduction introduction) {
        Account currentUser = TokenUtils.getCurrentUser();
        introduction.setUserId(currentUser.getId());
        introduction.setTime(DateUtil.now());
        introductionMapper.insert(introduction);
    }

    public void update(Introduction introduction) {
        introductionMapper.updateById(introduction);
    }

    public void deleteById(Integer id) {
        introductionMapper.deleteById(id);
    }


    public Introduction selectById(Integer id) {
        Introduction introduction = introductionMapper.selectById(id);
        Integer categoryId = introduction.getCategoryId();
        Integer userId = introduction.getUserId();
        Category category= categoryMapper.selectById(categoryId);
        User user = userMapper.selectById(userId.toString());
        if (ObjectUtil.isNotEmpty(category)){
            introduction.setCategoryTitle(category.getTitle());
        }
        if (ObjectUtil.isNotEmpty(user)){
            introduction.setUserName(user.getName());
            introduction.setUserAvatar(user.getAvatar());
        }
        return introduction;
    }
}
